package com.diabeco.ui.common;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.widget.TimePicker;

public class TimePickerFragment extends DialogFragment implements
		TimePickerDialog.OnTimeSetListener {

	public static final String INITIAL_HOUR_OF_DAY = TimePickerFragment.class
			.getName() + "initialHour";
	public static final String INITIAL_MINUTE = TimePickerFragment.class
			.getName() + "initialMinute";
	private TimePickerDialog.OnTimeSetListener activity;

	public TimePickerFragment() {
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		int hour = getArguments().getInt(INITIAL_HOUR_OF_DAY);
		int minute = getArguments().getInt(INITIAL_MINUTE);

		// Create a new instance of TimePickerDialog and return it
		return new TimePickerDialog(getActivity(), this, hour, minute,
				DateFormat.is24HourFormat(getActivity()));
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		try {
			this.activity = (OnTimeSetListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement OnTimeSetListener");
		}
	}

	@Override
	public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
		this.activity.onTimeSet(view, hourOfDay, minute);
	}

}